package cn.ywang.mybatis;

import cn.ywang.mybatis.mapper.TestTaskMapper;
import cn.ywang.mybatis.model.TestTask;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author: qingshan
 * @Date: 2019/1/16 13:44
 * @Description: 咕泡学院，只为更好的你
 * MyBatis Maven演示工程
 */
public class MyBatisTest {

	/**
	 * 逻辑分页
	 *
	 * @throws IOException
	 */
	@Test
	public void testSelectByRowBounds() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		SqlSession session = sqlSessionFactory.openSession();
		try {
			TestTaskMapper mapper = session.getMapper(TestTaskMapper.class);
			// offset
			int start = 1;
			// limit
			int pageSize = 1;
			RowBounds rb = new RowBounds(start, pageSize);
			// 使用逻辑分页
			List<TestTask> list = mapper.selectTaskList(rb);
			for (TestTask b : list) {
				System.out.println(b);
			}
		} finally {
			session.close();
		}
	}

}
